package com.example.mydataaccesssample;



import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActToDo extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acttodo);
		InitialComponent();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_to_do, menu);
		return true;
	}
	
	OnClickListener btnToDoAdd_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			SharedPreferences table = getSharedPreferences("TTODO", 0);
			//取得最大的數值
			int intMax=table.getInt("K_COUNT",0);
			//新的一筆加1
			intMax++;
			table.edit().putInt("K_COUNT", intMax).commit();
			String strKDate="KDATE"+String.valueOf(intMax);
			String strKTODO="KTODO"+String.valueOf(intMax);
			table.edit().putString(strKDate, txtToDoDate.getText().toString()).commit();
			table.edit().putString(strKTODO, txtToDoWaitTask.getText().toString()).commit();
						
			Toast.makeText(ActToDo.this, "儲存資料成功", Toast.LENGTH_SHORT).show();
	}};
	
	DialogInterface.OnClickListener onSelect_click=new DialogInterface.OnClickListener(){
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			
			String result[] = new String[2];
			
			SharedPreferences table = getSharedPreferences("TTODO", 0);
			result[0] = table.getString("KDATE"+arg1,"default text").toString();
			result[1] = table.getString("KTODO"+arg1,"default text").toString();

			
			txtToDoDate.setText(result[0]);
			txtToDoWaitTask.setText(result[1]);
	}};
	
	DialogInterface.OnClickListener onSelectDelete_click=new DialogInterface.OnClickListener(){
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			
			String result[] = new String[2];
			
			SharedPreferences table = getSharedPreferences("TTODO", 0);
			result[0] = table.getString("KDATE"+arg1,"default text").toString();
			result[1] = table.getString("KTODO"+arg1,"default text").toString();
			
			txtToDoDate.setText(result[0]);
			txtToDoWaitTask.setText(result[1]);
			//刪除這筆資料
			table.edit().remove(("KDATE"+(arg1)).toString()).commit();
			table.edit().remove(("KTODO"+(arg1)).toString()).commit();
			
			viewMessage.setText(result[0]+"這筆會議紀錄被刪除");
			
	}};	

	OnClickListener btnToDoDelete_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			SharedPreferences table = getSharedPreferences("TTODO", 0);
			int intMax=table.getInt("K_COUNT",0);
			
			String[] list = new String[intMax];
			for(int i= 0 ; i<intMax;i++){
				list[i] = table.getString("KDATE"+i,"default text").toString();
				list[i] += table.getString("KTODO"+i,"default text").toString();
			}

			
			Builder build=new Builder(ActToDo.this);
			build.setTitle("請選擇哪個會議要刪除：");
			build.setItems(list, onSelectDelete_click);
			Dialog dlg=build.create();
			dlg.show();
			
	}};
	
	
	OnClickListener btnToDoReload_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			SharedPreferences table = getSharedPreferences("TTODO", 0);
			int intMax=table.getInt("K_COUNT",0);
			
			String[] list = new String[intMax];
			for(int i= 0 ; i<intMax;i++){
				list[i] = table.getString("KDATE"+i,"default text").toString()+"\t";
				list[i] += table.getString("KTODO"+i,"default text").toString();
			}
			//彈跳視窗
			Builder build=new Builder(ActToDo.this);
			build.setTitle("請選擇哪個會議：");
			build.setItems(list, onSelect_click);
			Dialog dlg=build.create();
			dlg.show();
	}};
	
	OnDateSetListener date_Click = new OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			txtToDoDate.setText(year +"年"+ (monthOfYear+1) +"月"+ dayOfMonth+"日");
		}
	};
	
	OnClickListener btnDate_click=new OnClickListener(){
		public void onClick(View arg0) {
			//Java內建取得時間
			Calendar calendar = Calendar.getInstance();
			DatePickerDialog myDate = new DatePickerDialog
					(ActToDo.this,
					 date_Click, 
					 calendar.get(calendar.YEAR),
					 calendar.get(calendar.MONTH),
					 calendar.get(calendar.DATE)
					);
			myDate.show();
			
	}};
	
	private void InitialComponent() {
		
		SharedPreferences table = getSharedPreferences("TTODO", 0);
		table.edit().clear();
		
		viewToDoDate=(Button)findViewById(R.id.viewToDoDate);
		viewToDoDate.setOnClickListener(btnDate_click);
		
		viewToDoWaitTask=(TextView)findViewById(R.id.viewToDoWaitTask);
		
		txtToDoDate = (EditText)findViewById(R.id.txtToDoDate);
		txtToDoWaitTask = (EditText)findViewById(R.id.txtToDoWaitTask);
		
		btnToDoAdd = (Button)findViewById(R.id.btnToDoAdd);
		btnToDoAdd.setOnClickListener(btnToDoAdd_click);
		
		btnToDoReload = (Button)findViewById(R.id.btnToDoReload);
		btnToDoReload.setOnClickListener(btnToDoReload_click);
		
		btnToDelete = (Button)findViewById(R.id.btnToDelete);
		btnToDelete.setOnClickListener(btnToDoDelete_click);
		
		viewMessage = (TextView)findViewById(R.id.viewMessage);
		
	}
	
	
	
	Button viewToDoDate = null;
	TextView viewToDoWaitTask = null;
	
	EditText txtToDoDate=null;
	EditText txtToDoWaitTask=null;
	
	Button btnToDoAdd =null;
	Button btnToDoReload = null;

	Button btnToDelete = null;
	TextView viewMessage = null;
}
